package com.ruoyi.course.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.SysConstants;
import com.ruoyi.common.enums.CourseStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ObjectUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.course.domain.SysCourse;
import com.ruoyi.course.domain.SysCoursePaper;
import com.ruoyi.course.mapper.SysCoursePaperMapper;
import com.ruoyi.course.service.ISysCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.SysCourseStudentMapper;
import com.ruoyi.course.domain.SysCourseStudent;
import com.ruoyi.course.service.ISysCourseStudentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学员的课程信息Service业务层处理
 *
 * @author YJF
 * @date 2023-05-18
 */
@Service
public class SysCourseStudentServiceImpl implements ISysCourseStudentService
{
    @Autowired
    private SysCourseStudentMapper sysCourseStudentMapper;

    @Autowired
    private SysCoursePaperMapper sysCoursePaperMapper;
    @Autowired
    private ISysCourseService courseService;

    @Override
    public List<SysCourseStudent> getByStudentId(long uid){
        return sysCourseStudentMapper.getByStudentId(uid);
    }


    @Override
    public void saveCourseRate(SysCourseStudent courseStudent) {
        ObjectUtil.checkNull(courseStudent.getStudentId() ,"student id");
        ObjectUtil.checkNull(courseStudent.getCourseNo() ,"course no");
        List<SysCourseStudent> list = getByStudentId(courseStudent.getStudentId());
        for(SysCourseStudent student : list){
            if(courseStudent.getCourseNo().equals(student.getCourseNo())){
                updateSysCourseStudent(courseStudent);
                return;
            }
        }
        insertSysCourseStudent(courseStudent);
    }

    /**
     * 查询学员的课程信息
     *
     * @param id 学员的课程信息主键
     * @return 学员的课程信息
     */
    @Override
    public SysCourseStudent selectSysCourseStudentById(Long id)
    {
        return sysCourseStudentMapper.selectSysCourseStudentById(id);
    }

    /**
     * 查询学员的课程信息列表
     *
     * @param sysCourseStudent 学员的课程信息
     * @return 学员的课程信息
     */
    @Override
    public List<SysCourseStudent> selectSysCourseStudentList(SysCourseStudent sysCourseStudent) {

        if (StringUtils.isNull(sysCourseStudent.getDelFlag())) {
            sysCourseStudent.setDelFlag(SysConstants.CONSTANT_STR0);
        }
        List<SysCourseStudent> list = sysCourseStudentMapper.selectSysCourseStudentList(sysCourseStudent);
        for (SysCourseStudent l : list) {
            if (l.getBeginTime() != null && l.getFinishTime() != null) {
                //时长
                long diffTime = DateUtils.getMinuteDiff(l.getFinishTime(), l.getBeginTime());
                l.setStudyTime(diffTime);
                //学习进度
                if(StrUtil.isBlank(l.getStudySchedule())){
                    if (diffTime > 0) {
                        if (diffTime >= l.getTimeLength()) {
                            l.setStudySchedule("100");
                        } else {
                            String e = String.format("%.2f", (((double) diffTime / (double) l.getTimeLength()) * 100));
                            l.setStudySchedule(e);
                        }
                    } else {
                        l.setStudySchedule("0");
                    }
                }
            } else {
                l.setStudyTime(0L);
                l.setStudySchedule("0");
            }
        }
        return list;
    }

    /**
     * 新增学员的课程信息
     *
     * @param sysCourseStudent 学员的课程信息
     * @return 结果
     */
    @Override
    public int insertSysCourseStudent(SysCourseStudent sysCourseStudent)
    {
        //课程试卷的总时长
        if(StringUtils.isNull(sysCourseStudent.getTimeLength())){
            SysCoursePaper coursePaper = new SysCoursePaper();
            coursePaper.setDelFlag(SysConstants.CONSTANT_STR0);
            coursePaper.setCourseNo(sysCourseStudent.getCourseNo());
            SysCoursePaper paperInfo = sysCoursePaperMapper.selectSysCoursePaperByCoursePaperId(coursePaper);
            if (StringUtils.isNotNull(paperInfo)){
                sysCourseStudent.setTimeLength(paperInfo.getElapsedTime());
            }
        }
        SysCourse tmp = new SysCourse();
        tmp.setCourseNo(sysCourseStudent.getCourseNo());
        List<SysCourse> courses = courseService.selectSysCourseList(tmp);
        if(courses != null && !courses.isEmpty()){
            sysCourseStudent.setCourseName(courses.get(0).getCourseName());
        }
        sysCourseStudent.setCreateTime(DateUtils.getNowDate());
        return sysCourseStudentMapper.insertSysCourseStudent(sysCourseStudent);
    }

    @Override
    public SysCourseStudent getByUserIdAndCourseNo(Long userId, Long courseNo) {
        return sysCourseStudentMapper.getByUserIdAndCourseNo(userId,courseNo);
    }



    @Override
    @Transactional
    public SysCourseStudent updateSysCourseStudentProcess(SysCourseStudent sysCourseStudent) {
        ObjectUtil.checkNull(sysCourseStudent.getUserId() ,"未登录");
        ObjectUtil.checkNull(sysCourseStudent.getCourseNo() ,"课程编号为空");
        SysCourseStudent dbSysCourseStudent = sysCourseStudentMapper.getByUserIdAndCourseNo(sysCourseStudent.getUserId(),sysCourseStudent.getCourseNo());
        if (dbSysCourseStudent == null || dbSysCourseStudent.getId() == null) {
            sysCourseStudent.setCreateTime(DateUtils.getNowDate());
            sysCourseStudent.setDelFlag("0");
            if (sysCourseStudent.getScores() == null || sysCourseStudent.getScores() == 0) {
                sysCourseStudent.setScores(0L);
            }
            sysCourseStudent.setStudentId(sysCourseStudent.getUserId());
            sysCourseStudent.setStudyStatus(2);
            sysCourseStudent.setStudySchedule(StringUtils.getPercentage(sysCourseStudent.getStudyTime(),sysCourseStudent.getTimeLength())+"%");
            sysCourseStudentMapper.insertSysCourseStudent(sysCourseStudent);
            dbSysCourseStudent = sysCourseStudent;
        } else {
            if (dbSysCourseStudent.getScores() == null || dbSysCourseStudent.getScores() == 0) {
                sysCourseStudent.setScores(sysCourseStudent.getScores());
            }
            dbSysCourseStudent.setStudyTime(sysCourseStudent.getStudyTime());
            if (sysCourseStudent.getStudyStatus() == null || sysCourseStudent.getStudyStatus() == 1) {
                sysCourseStudent.setStudyStatus(2);
            }
            if (dbSysCourseStudent.getStudyStatus() != 3) {
                dbSysCourseStudent.setStudyStatus(sysCourseStudent.getStudyStatus());
            }
            dbSysCourseStudent.setTimeLength(sysCourseStudent.getTimeLength());
            dbSysCourseStudent.setBeginTime(sysCourseStudent.getBeginTime());
            dbSysCourseStudent.setFinishTime(sysCourseStudent.getFinishTime());
            dbSysCourseStudent.setLastTime(sysCourseStudent.getLastTime());
            dbSysCourseStudent.setStudySchedule(StringUtils.getPercentage(sysCourseStudent.getStudyTime(),sysCourseStudent.getTimeLength())+"%");
            sysCourseStudentMapper.updateSysCourseStudent(dbSysCourseStudent);
        }
        return dbSysCourseStudent;
    }

    /**
     * 修改学员的课程信息
     *
     * @param sysCourseStudent 学员的课程信息
     * @return 结果
     */
    @Override
    public int updateSysCourseStudent(SysCourseStudent sysCourseStudent)
    {
        sysCourseStudent.setUpdateTime(DateUtils.getNowDate());
        SysCourseStudent info = sysCourseStudentMapper.selectSysCourseStudentById(sysCourseStudent.getId());
        //如果学习状态有改变
        if(sysCourseStudent.getStudyStatus() != null) {
            if (!sysCourseStudent.getStudyStatus().equals(info.getStudyStatus())) {
                //进行中
                if (sysCourseStudent.getStudyStatus().equals(CourseStatus.AFOOT.getValue())) {
                    sysCourseStudent.setBeginTime(DateUtils.getNowDate());
                }
                //已完成
                if (sysCourseStudent.getStudyStatus().equals(CourseStatus.UNDERWAY.getValue())) {
                    sysCourseStudent.setFinishTime(DateUtils.getNowDate());
                }
                sysCourseStudent.setLastTime(DateUtils.getNowDate());
            }
        }
        return sysCourseStudentMapper.updateSysCourseStudent(sysCourseStudent);
    }

    /**
     * 批量删除学员的课程信息
     *
     * @param ids 需要删除的学员的课程信息主键
     * @return 结果
     */
    @Override
    public int deleteSysCourseStudentByIds(Long[] ids)
    {
        return sysCourseStudentMapper.deleteSysCourseStudentByIds(ids);
    }

    /**
     * 删除学员的课程信息信息
     *
     * @param id 学员的课程信息主键
     * @return 结果
     */
    @Override
    public int deleteSysCourseStudentById(Long id)
    {
        return sysCourseStudentMapper.deleteSysCourseStudentById(id);
    }
}
